﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace TaskListManager
{
   class TaskUserInterface
   {
      protected UserList m_sUsers;
      protected TaskList m_sTasks;

      public TaskUserInterface()
      {
         m_sUsers = new UserList();
         m_sTasks = new TaskList();
      }

      public int AddTask(string strDesc, long lStartDate, long lEndDate, short nPercentComplete, bool bCompleted, int lOwner)
      {
         bool bOwnerExists = m_sUsers.UserExists(lOwner);
         int lTaskId = -1;
         if (bOwnerExists == true)
         {
            lTaskId = m_sTasks.AddTask(strDesc, lStartDate, lEndDate, nPercentComplete, bCompleted, lOwner);
         }
         return lTaskId;
      }

      public void MoveTaskFromUser(int lCurrentUser, int lNewUser, int lTaskId)
      {
         bool bNewExists = m_sUsers.UserExists(lNewUser);
         if (bNewExists == true)
         {
            m_sTasks.ModifyTaskOwner(lTaskId, lNewUser);
         }
      }
      
      public void ModifyTaskDesc(int lTaskId, string strDesc)
      {
         m_sTasks.ModifyTaskDesc(lTaskId, strDesc);
      }

      public void ModifyTaskStartDate(int lTaskId, long lStart)
      {
         m_sTasks.ModifyTaskStartDate(lTaskId, lStart);
      }

      public void ModifyTaskEndDate(int lTaskId, long lEnd)
      {
         m_sTasks.ModifyTaskEndDate(lTaskId, lEnd);
      }

      public void ModifyTaskPercentComplete(int lTaskId, short nComplete)
      {
         m_sTasks.ModifyTaskPercentComplete(lTaskId, nComplete);
      }

      public void ModifyTaskCompleted(int lTaskId, bool bCompleted)
      {
         m_sTasks.ModifyTaskCompleted(lTaskId, bCompleted);
      }

      public void RemoveTaskForUser(int lUserId, int lTaskId)
      {
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).TaskId == lTaskId))
            {
               m_sTasks.RemoveTask(lTaskId);
               break;
            }
         }
      }

      public void RemoveAllTasksForUser(int lUserId)
      {
         int lTaskCount = 0;
         for (int i=0; i<m_sTasks.GetTaskCount(); i++)
         {
            if (m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId)
            {
               lTaskCount++;
            }
         }
         if (lTaskCount > 0)
         {
            int[] lTaskIds = new int[lTaskCount];
            int lIdx = 0;
            for (int i=0; i<m_sTasks.GetTaskCount(); i++)
            {
               if (m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId)
               {
                  lTaskIds[lIdx] = m_sTasks.GetTaskIdAtIndex(i);
                  lIdx++;
               }
            }
            for (int i=0; i<lTaskCount; i++)
            {
               m_sTasks.RemoveTask(lTaskIds[i]);
            }
         }
      }

      public TaskEntry GetTaskWithId(int lTaskId)
      {
         TaskEntry sEntry = new TaskEntry();
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if (m_sTasks.GetTaskIdAtIndex(i) == lTaskId)
            {
               sEntry = m_sTasks.GetTaskAtIndex(i);
               break;
            }
         }
         return sEntry;
      }

      public TaskEntry[] GetAllTasks()
      {
         TaskEntry[] sTasks = new TaskEntry[m_sTasks.GetTaskCount()];
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            sTasks[i] = m_sTasks.GetTaskAtIndex(i);
         }
         return sTasks;
      }

      public TaskEntry[] GetAllIncompleteTasks()
      {
         TaskEntry[] sEntries = new TaskEntry[1];
         sEntries[0] = new TaskEntry();
         int lCount = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if (m_sTasks.GetTaskAtIndex(i).Complete == false)
            {
               lCount++;
            }
         }
         if (lCount > 0)
         {
            sEntries = new TaskEntry[lCount];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if (m_sTasks.GetTaskAtIndex(i).Complete == false)
               {
                  sEntries[lIdx] = m_sTasks.GetTaskAtIndex(lIdx);
                  lIdx++;
               }
            }
         }
         return sEntries;
      }

      public TaskEntry[] GetAllCompleteTasks()
      {
         TaskEntry[] sEntries = new TaskEntry[1];
         sEntries[0] = new TaskEntry();
         int lCount = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if (m_sTasks.GetTaskAtIndex(i).Complete == true)
            {
               lCount++;
            }
         }
         if (lCount > 0)
         {
            sEntries = new TaskEntry[lCount];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if (m_sTasks.GetTaskAtIndex(i).Complete == true)
               {
                  sEntries[lIdx] = m_sTasks.GetTaskAtIndex(lIdx);
                  lIdx++;
               }
            }
         }
         return sEntries;
      }

      public TaskEntry[] GetAllTasksForUser(int lUserId)
      {
         TaskEntry[] sEntries = new TaskEntry[1];
         sEntries[0] = new TaskEntry();
         int lCount = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if (m_sTasks.GetTaskIdAtIndex(i) == lUserId)
            {
               lCount++;
            }
         }
         if (lCount > 0)
         {
            sEntries = new TaskEntry[lCount];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if (m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId)
               {
                  sEntries[lIdx] = m_sTasks.GetTaskAtIndex(lIdx);
                  lIdx++;
               }
            }
         }
         return sEntries;
      }

      public TaskEntry[] GetAllIncompleteTasksForUser(int lUserId)
      {
         TaskEntry[] sEntries = new TaskEntry[1];
         sEntries[0] = new TaskEntry();
         int lCount = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if ((m_sTasks.GetTaskIdAtIndex(i) == lUserId) && (m_sTasks.GetTaskAtIndex(i).Complete == false))
            {
               lCount++;
            }
         }
         if (lCount > 0)
         {
            sEntries = new TaskEntry[lCount];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if ((m_sTasks.GetTaskIdAtIndex(i) == lUserId) && (m_sTasks.GetTaskAtIndex(i).Complete == false))
               {
                  sEntries[lIdx] = m_sTasks.GetTaskAtIndex(lIdx);
                  lIdx++;
               }
            }
         }
         return sEntries;
      }

      public TaskEntry[] GetAllCompleteTasksForUser(int lUserId)
      {
         TaskEntry[] sEntries = new TaskEntry[1];
         sEntries[0] = new TaskEntry();
         int lCount = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if ((m_sTasks.GetTaskIdAtIndex(i) == lUserId) && (m_sTasks.GetTaskAtIndex(i).Complete == true))
            {
               lCount++;
            }
         }
         if (lCount > 0)
         {
            sEntries = new TaskEntry[lCount];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if ((m_sTasks.GetTaskIdAtIndex(i) == lUserId) && (m_sTasks.GetTaskAtIndex(i).Complete == true))
               {
                  sEntries[lIdx] = m_sTasks.GetTaskAtIndex(lIdx);
                  lIdx++;
               }
            }
         }
         return sEntries;
      }

      public TaskEntry[] GetAllTasksStartedAfterDate(long lStartDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if (m_sTasks.GetTaskAtIndex(i).StartDate > lStartDate)
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i=0; i<m_sTasks.GetTaskCount(); i++)
            {
               if (m_sTasks.GetTaskAtIndex(i).StartDate > lStartDate)
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public TaskEntry[] GetAllTasksEndedAfterDate(long lEndDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if (m_sTasks.GetTaskAtIndex(i).EndDate > lEndDate)
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if (m_sTasks.GetTaskAtIndex(i).EndDate > lEndDate)
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public TaskEntry[] GetAllTasksStartedBeforeDate(long lStartDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if (m_sTasks.GetTaskAtIndex(i).StartDate < lStartDate)
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if (m_sTasks.GetTaskAtIndex(i).StartDate < lStartDate)
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public TaskEntry[] GetAllTasksEndedBeforeDate(long lEndDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if (m_sTasks.GetTaskAtIndex(i).EndDate < lEndDate)
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if (m_sTasks.GetTaskAtIndex(i).EndDate < lEndDate)
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public TaskEntry[] GetAllTasksStartedBetweenDates(long lStartDate, long lEndDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if ((m_sTasks.GetTaskAtIndex(i).StartDate > lStartDate) && (m_sTasks.GetTaskAtIndex(i).StartDate < lEndDate))
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if ((m_sTasks.GetTaskAtIndex(i).StartDate > lStartDate) && (m_sTasks.GetTaskAtIndex(i).StartDate < lEndDate))
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public TaskEntry[] GetAllTasksEndedBetweenDates(long lStartDate, long lEndDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if ((m_sTasks.GetTaskAtIndex(i).EndDate > lStartDate) && (m_sTasks.GetTaskAtIndex(i).EndDate < lEndDate))
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if ((m_sTasks.GetTaskAtIndex(i).EndDate > lStartDate) && (m_sTasks.GetTaskAtIndex(i).EndDate < lEndDate))
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public TaskEntry[] GetTasksForUserStartedAfterDate(int lUserId, long lStartDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).StartDate > lStartDate))
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).StartDate > lStartDate))
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public TaskEntry[] GetTasksForUserEndedAfterDate(int lUserId, long lEndDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).EndDate > lEndDate))
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).EndDate > lEndDate))
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public TaskEntry[] GetTasksForUserStartedBeforeDate(int lUserId, long lStartDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).StartDate < lStartDate))
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).StartDate < lStartDate))
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public TaskEntry[] GetTasksForUserEndedBeforeDate(int lUserId, long lEndDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).EndDate < lEndDate))
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).EndDate < lEndDate))
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public TaskEntry[] GetTasksForUserStartedBetweenDates(int lUserId, long lStartDate, long lEndDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).StartDate > lStartDate) && (m_sTasks.GetTaskAtIndex(i).StartDate < lEndDate))
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).StartDate > lStartDate) && (m_sTasks.GetTaskAtIndex(i).StartDate < lEndDate))
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public TaskEntry[] GetTasksForUserEndedBetweenDates(int lUserId, long lStartDate, long lEndDate)
      {
         TaskEntry[] sTasks = new TaskEntry[1];
         int lCnt = 0;
         for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
         {
            if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).EndDate > lStartDate) && (m_sTasks.GetTaskAtIndex(i).EndDate < lEndDate))
            {
               lCnt++;
            }
         }
         if (lCnt > 0)
         {
            sTasks = new TaskEntry[lCnt];
            int lIdx = 0;
            for (int i = 0; i < m_sTasks.GetTaskCount(); i++)
            {
               if ((m_sTasks.GetTaskAtIndex(i).OwnerUser == lUserId) && (m_sTasks.GetTaskAtIndex(i).EndDate > lStartDate) && (m_sTasks.GetTaskAtIndex(i).EndDate < lEndDate))
               {
                  sTasks[lIdx] = m_sTasks.GetTaskAtIndex(i);
                  lIdx++;
               }
            }
         }
         return sTasks;
      }

      public bool TaskExists(int lTaskId)
      {
         return (m_sTasks.GetTaskIndexForId(lTaskId) != -1);
      }

      public string[] GetUserNames()
      {
         string[] strUsers = new string[1];
         strUsers[0] = "";
         if (m_sUsers.GetUserCount() > 0)
         {
            strUsers = new string[m_sUsers.GetUserCount()];
            for (int i = 0; i < m_sUsers.GetUserCount(); i++)
            {
               strUsers[i] = m_sUsers.GetUserAtIndex(i);
            }
         }
         return strUsers;
      }

      public int GetUserIdFromName(string strName)
      {
         return m_sUsers.GetUserId(strName);
      }

      public string GetUserNameFromId(int lId)
      {
         return m_sUsers.GetUserName(lId);
      }

      public bool AddUser(string strName)
      {
         bool bAdded = (m_sUsers.UserExists(strName) == false);
         if (bAdded == true)
         {
            m_sUsers.AddUser(strName);
         }
         return bAdded;
      }

      public void RemoveUser(string strName)
      {
         m_sUsers.RemoveUser(strName);
         int lUserId = GetUserIdFromName(strName);
         RemoveAllTasksForUser(lUserId);
      }

      public void RemoveUser(int lUserId)
      {
         m_sUsers.RemoveUser(lUserId);
         RemoveAllTasksForUser(lUserId);
      }

      public void RenameUser(int lUserId, string strName)
      {
         if (m_sUsers.UserExists(strName) == false)
         {
            m_sUsers.RenameUser(lUserId, strName);
         }
      }

      public bool UserExists(string strName)
      {
         return m_sUsers.UserExists(strName);
      }

      public bool UserExists(int lUID)
      {
         return m_sUsers.UserExists(lUID);
      }

      public void LoadFromDB()
      {
         try
         {
            System.IO.FileStream sTaskRdr = new System.IO.FileStream("TaskList.agt", System.IO.FileMode.Open);
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sTaskFmtr = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            object sTaskObj = sTaskFmtr.Deserialize(sTaskRdr);
            m_sTasks = (TaskList) sTaskObj;
            sTaskRdr.Close();
         }
         catch (Exception e)
         {
            System.Windows.Forms.MessageBox.Show(e.ToString(), "Error Loading Tasks");
         }
         try
         {
            System.IO.FileStream sUserRdr = new System.IO.FileStream("UserList.agt", System.IO.FileMode.Open);
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sUserFmtr = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            object sUserObj = sUserFmtr.Deserialize(sUserRdr);
            m_sUsers = (UserList) sUserObj;
            sUserRdr.Close();
         }
         catch (Exception e)
         {
            System.Windows.Forms.MessageBox.Show(e.ToString(), "Error Loading Users");
         }
      }

      public void SaveToDB()
      {
         try
         {
            System.IO.FileStream sTaskWrt = new System.IO.FileStream("TaskList.agt", System.IO.FileMode.Create);
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sTaskFmtr = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            sTaskFmtr.Serialize(sTaskWrt, m_sTasks);
            sTaskWrt.Close();
         }
         catch (Exception e)
         {
            System.Windows.Forms.MessageBox.Show(e.ToString(), "Error Saving Tasks");
         }

         try
         {
            System.IO.FileStream sUserWrt = new System.IO.FileStream("UserList.agt", System.IO.FileMode.Create);
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sUserFmtr = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            sUserFmtr.Serialize(sUserWrt, m_sUsers);
            sUserWrt.Close();
         }
         catch (Exception e)
         {
            System.Windows.Forms.MessageBox.Show(e.ToString(), "Error Saving Users");
         }
      }
   }
}
